package com.ff.xyh.service.userfront.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.servicebase.exception.MyException;
import com.ff.xyh.common.utils.DateTimeUtil;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.FrontUser;
import com.ff.xyh.common.entity.userfront.UserRestricted;
import com.ff.xyh.service.userfront.service.FrontUserService;
import com.ff.xyh.service.userfront.service.UserRestrictedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 用户限制表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/front-user")
public class UserRestrictedController {
    @Autowired
    private UserRestrictedService userRestrictedService;

    @Autowired
    private FrontUserService frontUserService;

    /**
     * 用户限制添加或更新 需要通过消费者将参数补充完整调用
     * @param userRestricted
     * @return
     */
    @RequestMapping("user-restrict")
    public ResultObject userRestrict(UserRestricted userRestricted) {
        if ( userRestrictedService.getById(userRestricted.getUserId()) == null){
            if (userRestrictedService.save(userRestricted)) {
                return ResultObject.ok().message("已将用户封号至: "
                        + DateTimeUtil.stampToTime(userRestricted.getGmtEffective().getTime())
                );
            } else {
                return ResultObject.error().message("封号失败");
            }
        } else {
            // 用户可能是封号状态也可能是封过号但已过期， 可以进行增加时长或者重新封号的操作
            // 这里采用重新封号
            if (userRestrictedService.updateUserRestricted(userRestricted))
                return ResultObject.ok().message("已将用户封号至: "
                        + DateTimeUtil.stampToTime(userRestricted.getGmtEffective().getTime()));
            else
                // 未成功修改 按原时间封号
                return ResultObject.error().message("封号信息修改失败, 用户将于："
                        + DateTimeUtil.stampToTime(
                                userRestrictedService.getById(userRestricted.getUserId()).getGmtEffective().getTime())
                        + " 解封");
        }
    }

    /**
     * 用户封号信息删除
     * 通过消费者调用 需要同时修改前端用户表的信息
     * @param userRestricted
     * @return
     */
    @RequestMapping("user-restrict-delete")
    public ResultObject userRestrictDelete(UserRestricted userRestricted) {
        userRestrictedService.removeById(userRestricted);
        FrontUser frontUser = frontUserService.getById(userRestricted.getUserId());
        if (frontUser == null) {
            throw new MyException(20001, "用户不存在");
        } else {
            // 将用户状态改成正常 成功后才能删除封号信息
            if ( ! frontUserService.updateById(frontUser.setStatus(0))) {
                throw new MyException(20001, "解封失败");
            } else {
                if ( ! userRestrictedService.updateUserRestricted(userRestricted))
                    throw new MyException(20001, "解封信息修改失败");
                else if (! userRestrictedService.remove(new QueryWrapper<UserRestricted>()
                            .eq("user_id", userRestricted.getUserId())))
                    throw new MyException(20001, "解封信息删除失败");
                else
                    return ResultObject.ok().message("账号解封成功");
            }

        }
    }


}

